package server.model.players.skills;

import server.Config;
import server.model.players.Client;
import server.util.Misc;

/**
 * Fishing.java
 * 
 * @author Sanity
 * 
 **/

public class Fishing {

	private Client c;
	private int fishType;
	private int exp;
	private int req;
	private String name;
	private String equip;
	private int equipmentType;
	private final int SALMON_EXP = 70;
	private final int SWORD_EXP = 100;
	private final int SALMON_ID = 331;
	private final int SWORD_ID = 371;
	public boolean fishing = false;

	private final int[] REQS = { 1, 5, 35, 40, 50, 76, 81 };
	private final int[] FISH_TYPES = { 317, 328, 359, 377, 371, 383, 389 };
	private final int[] EXP = { 10, 20, 80, 90, 100, 210, 216 };
	private final int[] EMOTE = { 621, 622, 618, 619, 618, 618, 620 };
	private final String[] NAMES = { "shrimp", "sardine", "tuna", "lobster",
			"swordfish", "shark", "manta ray" };
	private final String[] EQUIPS = { "small fishing net", "fishing rod", "harpoon", "lobster pot",
			"harpoon", "harpoon", "big fishing net" };

	public Fishing(Client c) {
		this.c = c;
	}

	public void setupFishing(int fishType) {
		if (c.getItems().playerHasItem(getEquipment(fishType))) {
			if (c.playerLevel[c.playerFishing] >= req) {
				if (c.getItems().freeSlots() > 0) {
					int slot = getSlot(fishType);
					if (slot > -1) {
						this.req = REQS[slot];
						this.fishType = FISH_TYPES[slot];
						this.equipmentType = getEquipment(fishType);
						this.exp = EXP[slot];
						this.name = NAMES[slot];
						this.equip = EQUIPS[slot];
						c.fishing = true;
						c.fishTimer = 3 + Misc.random(2);
						c.startAnimation(EMOTE[slot]);
					}
				} else {
					c.sendMessage("You do not have enough space to hold this fish.");
					resetFishing();
				}
			} else {
				c.sendMessage("You need a fishing level of " + req
						+ " to fish for " + name + ".");
				resetFishing();
			}
		} else {
			c
					.sendMessage("You need a "+equip+" to fish here.");
			resetFishing();
		}
	}

	public void catchFish() {
		if (c.getItems().playerHasItem(getEquipment(fishType))) {
			if (c.playerLevel[c.playerFishing] >= req) {
				if (c.getItems().freeSlots() > 0) {
					int slot = getSlot(fishType);
					if (canFishOther(fishType)) {
						c.getItems().addItem(otherFishId(fishType), 1);
						c.getPA().addSkillXP(otherFishXP(fishType),
								c.playerFishing);
					} else {
						c.getItems().addItem(fishType, 1);
						c.getPA().addSkillXP(exp * Config.FISHING_EXPERIENCE,
								c.playerFishing);
					}
					c.sendMessage("You catch a " + name + ".");
					c.fishTimer = 2 + Misc.random(2);
					c.startAnimation(EMOTE[slot]);
				} else {
					c.sendMessage("You don't have enough space to hold this fish.");
					resetFishing();
				}
			} else {
				c.sendMessage("You need a fishing level of " + req
						+ " to fish for " + name + ".");
				resetFishing();
			}
		} else {
			c
			.sendMessage("You need a "+equip+" to fish here.");
			resetFishing();
		}
	}

	private int getSlot(int fishType) {
		for (int j = 0; j < REQS.length; j++) {
			if (FISH_TYPES[j] == fishType) {
				return j;
			}
		}
		return -1;
	}

	private int getEquipment(int fish) {
		switch (fish) {
		case 377:
			return 301;
		case 383:
		case 372:
		case 359:
			return 311;
		case 328:
			return 307;
		case 317:
			return 303;
		case 389:
			return 305;
		default:
			return -1;
		}
	}

	private boolean canFishOther(int fishType) {
		if (fishType == 335 && c.playerLevel[c.playerFishing] >= 30) {
			return true;
		}
		if (fishType == 361 && c.playerLevel[c.playerFishing] >= 50) {
			return true;
		}
		return false;
	}

	private int otherFishId(int fishType) {
		if (fishType == 335) {
			return SALMON_ID;
		} else if (fishType == 361) {
			return SWORD_ID;
		}
		return -1;
	}

	private int otherFishXP(int fishType) {
		if (fishType == 335) {
			return SALMON_EXP;
		} else if (fishType == 361) {
			return SWORD_EXP;
		}
		return 0;
	}

	public void resetFishing() {
		this.exp = 0;
		this.fishType = -1;
		this.equipmentType = -1;
		this.req = 0;
		c.fishTimer = -1;
		c.fishing = false;
	}
}